package com.wdb007.venus.wss.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wdb007.venus.svc.biz.manager.ConfigInfoManager;
import com.wdb007.venus.svc.facade.BookShelfService;
import com.wdb007.venus.wss.channelgroup.BookCaseChannelGroups;
import com.wdb007.venus.wss.service.WebSocketServerService;

import io.netty.channel.ChannelHandlerContext;

@Service("bookCaseRemoteUpdateService")
public class BookCaseRemoteUpdateService implements WebSocketServerService {

	private static Logger logger = LoggerFactory.getLogger(BookCaseRemoteUpdateService.class);
	@Autowired
	private BookShelfService bookShelfService;
	@Autowired
	private ConfigInfoManager configInfoManager;
	
	@Override
	public String doService(ChannelHandlerContext ctx, String request) throws Exception {
		JSONObject jsonObj = JSON.parseObject(request);
		logger.info("BookCaseReq 接收websocket响应信息: {}", jsonObj.toJSONString());
		
		jsonObj.put("url", configInfoManager.getValue("ru_url"));
		String bookCaseId = jsonObj.getString("bookCaseId");
		jsonObj.remove("bookCaseId");
		
		String version = bookShelfService.getVersion(bookCaseId);
		jsonObj.put("ver", version);
		logger.info("给书柜[{}]发送远程更新信息[{}]", bookCaseId, jsonObj.toJSONString());
		BookCaseChannelGroups.broadcast(bookCaseId, jsonObj.toJSONString());
		return null;
	}

}
